home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 September / Macworld (1998-09).dmg / Shareware World / Info / For Developers / MacZoop 1.8.3 / More Classes / Plug-In Classes / ZPlugInHandler.cpp < prev    next >
Text File  |  1997-12-18  |  5KB  |  218 lines

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            MacZoop - "the framework for the rest of us"         
  5. *
  6. *
  7. *
  8. *            ZPlugInHandler.cp    -- an object for managing plug-ins
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1997, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZPlugInHandler.h"
  23. #include    "ZProgress.h"
  24. #include    "ZObjectArray.cpp"
  25. #include    "MacZoop.h"
  26.  
  27. /*------------------------------***  CONSTRUCTOR  ***-----------------------------------*/
  28.  
  29. ZPlugInHandler::ZPlugInHandler( const FSSpec& rootFolder )
  30.     : ZFolderScanner( rootFolder )
  31. {
  32.     FailNIL( itsPlugIns = new ZPlugInList());
  33. }
  34.  
  35.  
  36. /*-------------------------------***  DESTRUCTOR  ***-----------------------------------*/
  37.  
  38. ZPlugInHandler::~ZPlugInHandler()
  39. {
  40.     if ( itsPlugIns )
  41.     {
  42.         // close them all, then dispose of them
  43.         
  44.         ZPlugIn*    zp;
  45.         short        i;
  46.         
  47.         for( i = 1; i<= itsPlugIns->CountItems(); i++ )
  48.         {
  49.             zp = itsPlugIns->GetObject( i );
  50.             
  51.             if ( zp )
  52.                 zp->ClosePlugIn();
  53.         }
  54.         
  55.         itsPlugIns->DisposeAll();
  56.         ForgetObject( itsPlugIns );
  57.     }
  58. }
  59.  
  60.  
  61. /*------------------------------***  INITPLUGINS  ***-----------------------------------*/
  62. /*    
  63. called after making the object to actually load and initialise the plug-ins. The standard
  64. behaviour is to scan the nominated folder and pass each file to Process1File. For each file
  65. the relevant ZPlugIn derived object is made and added to the plug-in list.
  66. ----------------------------------------------------------------------------------------*/
  67.  
  68. void        ZPlugInHandler::InitPlugIns()
  69. {
  70.     ScanFolder();
  71.     
  72.     // after scanning, we have a list of raw unopened plug-ins. Now we need to
  73.     // initialise them and open them.
  74.     
  75.     ZPlugIn*    zp;
  76.     short        i;
  77.     
  78.     for( i = 1; i<= itsPlugIns->CountItems(); i++ )
  79.     {
  80.         zp = itsPlugIns->GetObject( i );
  81.         
  82.         if ( zp )
  83.             zp->InitPlugIn();
  84.     }
  85.     
  86.     // and open them all:
  87.     
  88.     for( i = 1; i<= itsPlugIns->CountItems(); i++ )
  89.     {
  90.         zp = itsPlugIns->GetObject( i );
  91.         
  92.         if ( zp )
  93.             zp->OpenPlugIn();
  94.     }
  95. }
  96.  
  97.  
  98. /*--------------------------***  SENDMESSAGETOPLUGIN  ***-------------------------------*/
  99. /*    
  100. passes the message and data to the plug-in with id passed. Plug-in IDs range from 1 and
  101. are generally in alphabetical order.
  102. ----------------------------------------------------------------------------------------*/
  103.  
  104. void        ZPlugInHandler::SendMessageToPlugIn( const short plugID, const long message, void* msgData )
  105. {
  106.     ZPlugIn*    zp;
  107.     
  108.     zp = itsPlugIns->GetObject( plugID );
  109.     
  110.     if ( zp )
  111.         zp->CallPlugIn( message, msgData );
  112. }
  113.  
  114.  
  115. /*----------------------------***  SENDMESSAGETOALL  ***--------------------------------*/
  116. /*
  117. sends the same message and data to all of the plug-ins.    
  118. ----------------------------------------------------------------------------------------*/
  119.  
  120. void        ZPlugInHandler::SendMessageToAll( const long message, void* msgData )
  121. {
  122.     short    i;
  123.     
  124.     for( i = 1; i <= itsPlugIns->CountItems(); i++ )
  125.         SendMessageToPlugIn( i, message, msgData );
  126. }
  127.  
  128.  
  129. /*--------------------------***  BUILDMENUOFPLUGINS  ***--------------------------------*/
  130. /*
  131. builds a menu of the names of the plug-ins. This is one (crude) way of making a user-
  132. interface to call the plug-ins from an application, though this is not recommended for
  133. sophisticated applications.    
  134. ----------------------------------------------------------------------------------------*/
  135.  
  136. void        ZPlugInHandler::BuildMenuOfPlugIns( MenuHandle aMenu )
  137. {
  138.     FailNILParam( aMenu );
  139.     
  140.     if ( itsPlugIns->CountItems() > 0 )
  141.     {
  142.         ZPlugIn*    zp;
  143.         Str255        plName;
  144.         short        i, n = CountMenuItems( aMenu );
  145.         
  146.         while( n )
  147.             DeleteMenuItem( aMenu, n-- );
  148.             
  149.         for ( i = 1; i <= itsPlugIns->CountItems(); i++ )
  150.         {
  151.             zp = itsPlugIns->GetObject( i );
  152.             
  153.             if ( zp )
  154.             {    
  155.                 zp->GetName( plName );
  156.                 
  157.                 AppendMenu( aMenu, "\px" );
  158.                 SetMenuItemText( aMenu, i, plName );
  159.             }
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165. /*------------------------------***  PROCESS1FILE  ***----------------------------------*/
  166. /*
  167. for each plug-in file, make a plug-in object and append to our list.    
  168. ----------------------------------------------------------------------------------------*/
  169.  
  170. void        ZPlugInHandler::Process1File( const FSSpec& aSpec, const OSType fType )
  171. {
  172.     // if using progress, set title to something suitable
  173.     
  174.     Str255    phTitle;
  175.     
  176.     if ( useProgressDialog )
  177.     {
  178.         GetIndString( phTitle, 202, 20 );
  179.         
  180.         itsPD->SetTitle( phTitle );
  181.     }
  182.     
  183.     // make a plug-in object and add it to the list
  184.     
  185.     ZPlugIn*    zp = MakePlugIn( aSpec, fType );
  186.     
  187.     if ( zp )
  188.     {
  189.         itsPlugIns->AppendItem( zp );
  190.         
  191.         // inherit progress bar behaviour
  192.         
  193.         ZFolderScanner::Process1File( aSpec, fType );
  194.     }
  195. }
  196.  
  197.  
  198. /*-------------------------------***  MAKEPLUGIN  ***-----------------------------------*/
  199. /*
  200. Make the plug-in object associated with the passed file. You need to override this method
  201. to make useful plug-in objects that implement your desired protocol and plug-in file
  202. format.    
  203. ----------------------------------------------------------------------------------------*/
  204.  
  205. ZPlugIn*    ZPlugInHandler::MakePlugIn( const FSSpec& aSpec, const OSType fType )
  206. {
  207.     ZPlugIn*    zp = NULL;
  208.     
  209.     // make the plug-in object. You need to override this to make objects of your desired
  210.     // kind. This makes generic ZPlugIns but they do nothing. Normally you'll check fType
  211.     // at least to see if the filetype is what you wanted. It is advisable to make other
  212.     // sanity checks on the file format as part of your plug-in protocol.
  213.     
  214.     FailNIL( zp = new ZPlugIn( aSpec ));
  215.     
  216.     return zp;
  217. }
  218.